<template>
  <div class="app-container">
    <div class="filter-container">
    </div>
    <div class="table-container">
      <Table :table="dataTable">
        <template
          slot-scope="props"
          slot="imgA"
        >
          <img
            :src="props.obj.row.imgA"
            alt=""
            @click="showImgDialogBox(props.obj.row.imgA)"
            class="small-img"
          >
        </template>
        <template
          slot-scope="props"
          slot="status"
        >
          <el-button
            type="text"
            @click="showcheckGuideBox(props.obj.row.id)"
            v-if="props.obj.row.status === 0"
          >待审核</el-button>
          <span v-else>
            {{props.obj.row.statusWord}}
          </span>
        </template>
      </Table>
      <div
        class="Pagination"
        style="text-align: left;margin-top: 10px;"
      >
        <el-pagination
          v-if="pageshow"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="limit"
          layout="total, prev, pager, next"
          :total="count"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog
      :visible.sync="showImgDialog"
      top="4%"
      width="40%"
    >
      <div class="demo-image__lazy">
        <div
          v-for="(pic,index) in picture"
          :key="index"
        >
          <vue-photo-zoom-pro :url="pic"></vue-photo-zoom-pro>
        </div>
      </div>
    </el-dialog>
    <el-dialog
      title="审核"
      :visible.sync="showcheckGuideDialog"
      top="4%"
      width="40%"
      center
    >
      <el-radio
        v-model="checkGuideData.radio"
        label="1"
      >审核通过</el-radio>
      <el-radio
        v-model="checkGuideData.radio"
        label="2"
      >审核不通过</el-radio>

      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="showcheckGuideDialog = false">取 消</el-button>
        <el-button
          type="primary"
          @click="postCheckGuide()"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { fetchQinqinList, fetchCheckGuide } from "@/api/table";
import format from "date-format";
import Table from "@/components/Table";
export default {
  data() {
    return {
      //总页码
      count: 0,
      //单页行数
      limit: 20,
      //页码
      currentPage: 1,
      searchInpt: "",
      searchLoading: false,
      pageshow: true,
      dataTable: {
        loading: false,
        border: true,
        hasOperation: false,
        tr: [
          {
            id: "1",
            label: "id",
            prop: "id",
            show: true
          },
          {
            id: "2",
            label: "用户昵称",
            prop: "nickName",
            show: true
          },
          {
            id: "3",
            label: "角色名称",
            prop: "roleName",
            show: true
          },
          {
            id: "4",
            label: "标签",
            prop: "labelName",
            show: true
          },
          {
            id: "5",
            label: "回复内容",
            prop: "content",
            show: true
          },
          {
            id: "6",
            label: "图片",
            prop: "imgA",
            show: "template"
          },
          {
            id: "7",
            label: "创建时间",
            prop: "createTime",
            show: true
          },
          {
            id: "8",
            label: "更新时间",
            prop: "updateTime",
            show: true
          },
          {
            id: "9",
            label: "操作",
            prop: "status",
            show: "template"
          }
        ],
        data: [],
        operation: {}
      },
      showImgDialog: false,
      picture: [],
      showcheckGuideDialog: false,
      checkGuideData: {
        id: "",
        radio: "1"
      }
    };
  },
  mounted() {
    this.initData();
  },
  methods: {
    initData() {
      this.getQinqinList();
    },
    showImgDialogBox(img) {
      if (img) {
        this.showImgDialog = true;
        this.picture = img.split(",");
      } else {
        this.$message({
          type: "error",
          message: "无图片"
        });
      }
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getQinqinList();
    },
    //时间格式化
    formatterTime(t) {
      if (t) {
        const newTime = format("yyyy-MM-dd hh:mm:ss", new Date(t)).split(" ");
        let time = "";
        time += newTime[0] + "\n" + newTime[1];
        return time;
      } else {
        return "";
      }
    },
    async getQinqinList() {
      this.dataTable.loading = true;
      this.searchLoading = true;
      const res = await fetchQinqinList();
      this.searchLoading = false;
      this.dataTable.loading = false;
      this.dataTable.data = [];
      this.count = res.data.length;
      res.data.forEach(item => {
        const data = {};
        data.id = item.id;
        data.labelName = item.labelName;
        data.nickName = item.nickName;
        data.roleName = item.roleName;
        data.content = item.content;
        data.imgA = item.imgA;
        data.status = item.status;
        if (item.status === 1) {
          data.statusWord = "已通过";
        } else if (item.status === 2) {
          data.statusWord = "未通过";
        }
        data.createTime = this.formatterTime(item.createTime);
        data.updateTime = this.formatterTime(item.updateTime);
        this.dataTable.data.push(data);
      });

      //前端分页
      this.dataTable.data = this.dataTable.data.slice(
        (this.currentPage - 1) * this.limit,
        this.currentPage * this.limit
      );
    },
    showcheckGuideBox(id) {
      this.showcheckGuideDialog = true;
      this.checkGuideData.id = id;
    },
    async postCheckGuide() {
      const res = await fetchCheckGuide({
        id: this.checkGuideData.id,
        status: this.checkGuideData.radio
      });
      this.getQinqinList();
      this.showcheckGuideDialog = false;
      this.$message({
        type: "success",
        message: "成功"
      });
    }
  },
  components: {
    Table
  }
};
</script>
<style lang="scss" scoped>
.small-img {
  width: 100px;
  cursor: pointer;
}
</style>
